package cn.piesat.controller;

import cn.piesat.service.intf.MenuService;
import cn.piesat.util.ResponseWrapperUtil;
import com.alibaba.fastjson.JSONArray;
import cn.piesat.vo.menu.AddMenuVO;
import cn.piesat.vo.menu.MenuDetailVO;
import cn.piesat.vo.menu.UpdateMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 菜单信息访问接口
 *
 * @author xhl
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加菜单信息
     * @param dto 添加菜单信息参数对象
     * @return
     */
    @PostMapping("add")
    public String add(@Valid @RequestBody AddMenuVO dto) {
        Map<String,String> resultMap = null;
        try {
            resultMap = menuService.add(dto);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","添加菜单，接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 修改菜单信息
     * @param updateDTO 修改菜单信息参数对象
     * @return
     */
    @PostMapping("update")
    public String update(@Valid @RequestBody UpdateMenuVO updateDTO) {
        Map<String,String> resultMap = null;
        try {
            resultMap = menuService.update(updateDTO);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","修改菜单，接口异常");
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 获取单个菜单的详细信息
     * @param id 菜单ID
     * @return
     */
    @GetMapping("get/{id}")
    public String getOne(@PathVariable("id") String id) {
        MenuDetailVO menuDetailVO = menuService.getOne(id);
        return ResponseWrapperUtil.executeResultEntity(menuDetailVO);
    }

    @GetMapping("list")
    public String list() {
        JSONArray resultArr = menuService.list();
        return ResponseWrapperUtil.executeResultList(resultArr);
    }

    @GetMapping("delete/{id}")
    public String deleteOne(@PathVariable("id") String id) {
        Map<String,String> resultMap = null;
        try {
            resultMap = menuService.deleteOne(id);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","删除菜单接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }
}
